import {h} from "vue";
import {MIcon} from "../components/index.js";
import iconComponent from "../plugins/xicons/index.js";
import {SettingsFilled} from "@vicons/material";

/**
 * /user 转换位 SysUser
 * @returns {*}
 */
export const convertPathToCamelCase = (input) => {
    // 使用正则表达式将每个子路径的首字母大写
    return input
        .split('/')
        .filter(Boolean) // 去除空字符串
        .map(word => word.charAt(0).toUpperCase() + word.slice(1))
        .join('')
        .split('-')
        .filter(Boolean) // 去除空字符串
        .map(word => word.charAt(0).toUpperCase() + word.slice(1))
        .join('');
}


/**
 * 图标渲染
 * @param iconComponent
 * @returns {function(): VNode<RendererNode, RendererElement, {[p: string]: any}>}
 */
export const renderIcon = (iconComponent) => {
    return () => h(MIcon, {
        icon: iconComponent
    })
}

export const renderIconByName = (iconName) => {
    return h(MIcon, {
        icon: iconComponent[iconName]
    })
}

/**
 * 后台的树装结构到前台变成菜单的树状结构
 * @param menus
 * @returns {*}
 */
export const transformMenuTree = (menus) => {
    return menus.map(menu => {
        const children = menu.children ? transformMenuTree(menu.children) : undefined
        return {
            label: menu.menuName,
            type: menu.menuType,
            key: menu.path ? convertPathToCamelCase(menu.path) : `${menu.id}`,
            icon: () => renderIconByName(menu.iconName),
            children
        }
    })
}
